package br.com.datasun.biblioteca.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import images.ImageLoader;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;
import java.awt.Insets;
import java.text.SimpleDateFormat;
import java.util.Date;

import br.com.datasun.biblioteca.entities.Emprestimo;
import br.com.datasun.biblioteca.entities.Exemplar;
import br.com.datasun.biblioteca.entities.Livro;
import br.com.datasun.biblioteca.entities.Pessoa;
import br.com.datasun.biblioteca.ui.controller.DevolucaoExemplarController;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class DevolucaoExemplarUI extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JButton jbLimpar = new JButton("");	
	private JButton jbFechar = new JButton("");
	private JButton jbConfirmar = new JButton("");
	private JLabel JlTituloTela = new JLabel("Devolu\u00E7\u00E3o de Livro");
	private JPanel jpDados = new JPanel();
	private JLabel jlCodExemplar = new JLabel("C\u00F3d. Exemplar");
	private JTextField JtCodExemplar = new JTextField();
	private JLabel jtTitulo = new JLabel("T\u00EDtulo");
	private JLabel jlTituloDescricao = new JLabel("Certifica\u00E7\u00E3o Programador Java 6");
	private JLabel jlISBN = new JLabel("ISBN");
	private JLabel jlISBNDescricao = new JLabel("1231231231-1");
	private JLabel jlPrevDevolucao = new JLabel("Prev Devolu\u00E7\u00E3o");
	private JLabel jlPrevDevolucaoDescricao = new JLabel("12/05/2012");
	private JLabel jlDataEmprestimo = new JLabel("Data Empr\u00E9stimo");
	private JLabel jlDataEmprestimoDescricao = new JLabel("05/05/2012");
	private JLabel jlDataDevolucao = new JLabel("Data Devolu\u00E7\u00E3o");
	private JLabel jlDataDevolucaoDescricao = new JLabel("12/05/2012");
	private JLabel jlCliente = new JLabel("Cliente");
	private JLabel jlCPF = new JLabel("CPF");
	private JLabel JlClienteDescricao = new JLabel("Douglas Jos\u00E9 Marlon");
	private JLabel jlCPFDescricao = new JLabel("222.222.222-22");
	
	private Emprestimo emprestimoSelecionado = null;
	
	private DevolucaoExemplarController controller;

	/**
	 * Launch the application.
	 */
	

	/**
	 * Create the dialog.
	 */
	public DevolucaoExemplarUI() {
		JtCodExemplar.setToolTipText("Informe o Exemplar e Tecle Enter");
		JtCodExemplar.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				char tecla = arg0.getKeyChar();
				if ((tecla == KeyEvent.VK_ENTER)&&(JtCodExemplar.getText().length() > 0)){
					if (jbConfirmar.isEnabled()) {
						controller.btnConfirmarEventHandler();
					}else{
						controller.pesquisarExemplarNaoDevolvido();
					}
				}
				if ((tecla == KeyEvent.VK_BACK_SPACE)||(tecla == KeyEvent.VK_DELETE)){
					controller.btnLimparEventHandler();
				}
			}
		});
		JtCodExemplar.setColumns(10);
		setTitle("Devolu\u00E7\u00E3o de Livro");
		setBounds(100, 100, 659, 438);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		JlTituloTela.setFont(new Font("Tahoma", Font.BOLD, 11));
		JlTituloTela.setHorizontalAlignment(SwingConstants.CENTER);
		
		contentPanel.add(JlTituloTela, BorderLayout.NORTH);
		
		contentPanel.add(jpDados, BorderLayout.CENTER);
		GridBagLayout gbl_jpDados = new GridBagLayout();
		gbl_jpDados.columnWidths = new int[]{0, 111, 174, 0};
		gbl_jpDados.rowHeights = new int[]{88, 30, 30, 30, 30, 30, 40, 30, 0};
		gbl_jpDados.columnWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
		gbl_jpDados.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		jpDados.setLayout(gbl_jpDados);
		
		GridBagConstraints gbc_jlCodExemplar = new GridBagConstraints();
		gbc_jlCodExemplar.insets = new Insets(0, 0, 5, 5);
		gbc_jlCodExemplar.anchor = GridBagConstraints.EAST;
		gbc_jlCodExemplar.gridx = 1;
		gbc_jlCodExemplar.gridy = 0;
		jlCodExemplar.setFont(new Font("Tahoma", Font.BOLD, 11));
		jpDados.add(jlCodExemplar, gbc_jlCodExemplar);
		
		GridBagConstraints gbc_JtCodExemplar = new GridBagConstraints();
		gbc_JtCodExemplar.insets = new Insets(0, 0, 5, 0);
		gbc_JtCodExemplar.fill = GridBagConstraints.HORIZONTAL;
		gbc_JtCodExemplar.gridx = 2;
		gbc_JtCodExemplar.gridy = 0;
		jpDados.add(JtCodExemplar, gbc_JtCodExemplar);
		
		GridBagConstraints gbc_jtTitulo = new GridBagConstraints();
		gbc_jtTitulo.anchor = GridBagConstraints.EAST;
		gbc_jtTitulo.insets = new Insets(0, 0, 5, 5);
		gbc_jtTitulo.gridx = 1;
		gbc_jtTitulo.gridy = 1;
		jtTitulo.setFont(new Font("Tahoma", Font.BOLD, 11));
		jpDados.add(jtTitulo, gbc_jtTitulo);
		
		GridBagConstraints gbc_jlTituloDescricao = new GridBagConstraints();
		gbc_jlTituloDescricao.insets = new Insets(0, 0, 5, 0);
		gbc_jlTituloDescricao.anchor = GridBagConstraints.WEST;
		gbc_jlTituloDescricao.gridx = 2;
		gbc_jlTituloDescricao.gridy = 1;
		jpDados.add(jlTituloDescricao, gbc_jlTituloDescricao);
		
		GridBagConstraints gbc_jlISBN = new GridBagConstraints();
		gbc_jlISBN.anchor = GridBagConstraints.EAST;
		gbc_jlISBN.insets = new Insets(0, 0, 5, 5);
		gbc_jlISBN.gridx = 1;
		gbc_jlISBN.gridy = 2;
		jlISBN.setFont(new Font("Tahoma", Font.BOLD, 11));
		jpDados.add(jlISBN, gbc_jlISBN);
		
		GridBagConstraints gbc_jlISBNDescricao = new GridBagConstraints();
		gbc_jlISBNDescricao.insets = new Insets(0, 0, 5, 0);
		gbc_jlISBNDescricao.anchor = GridBagConstraints.WEST;
		gbc_jlISBNDescricao.gridx = 2;
		gbc_jlISBNDescricao.gridy = 2;
		jpDados.add(jlISBNDescricao, gbc_jlISBNDescricao);
		
		GridBagConstraints gbc_jlPrevDevolucao = new GridBagConstraints();
		gbc_jlPrevDevolucao.anchor = GridBagConstraints.EAST;
		gbc_jlPrevDevolucao.insets = new Insets(0, 0, 5, 5);
		gbc_jlPrevDevolucao.gridx = 1;
		gbc_jlPrevDevolucao.gridy = 3;
		jlPrevDevolucao.setFont(new Font("Tahoma", Font.BOLD, 11));
		jpDados.add(jlPrevDevolucao, gbc_jlPrevDevolucao);
		
		GridBagConstraints gbc_jlPrevDevolucaoDescricao = new GridBagConstraints();
		gbc_jlPrevDevolucaoDescricao.insets = new Insets(0, 0, 5, 0);
		gbc_jlPrevDevolucaoDescricao.anchor = GridBagConstraints.WEST;
		gbc_jlPrevDevolucaoDescricao.gridx = 2;
		gbc_jlPrevDevolucaoDescricao.gridy = 3;
		jpDados.add(jlPrevDevolucaoDescricao, gbc_jlPrevDevolucaoDescricao);
		
		GridBagConstraints gbc_jlDataEmprestimo = new GridBagConstraints();
		gbc_jlDataEmprestimo.anchor = GridBagConstraints.EAST;
		gbc_jlDataEmprestimo.insets = new Insets(0, 0, 5, 5);
		gbc_jlDataEmprestimo.gridx = 1;
		gbc_jlDataEmprestimo.gridy = 4;
		jlDataEmprestimo.setFont(new Font("Tahoma", Font.BOLD, 11));
		jpDados.add(jlDataEmprestimo, gbc_jlDataEmprestimo);
		
		GridBagConstraints gbc_jlDataEmprestimoDescricao = new GridBagConstraints();
		gbc_jlDataEmprestimoDescricao.insets = new Insets(0, 0, 5, 0);
		gbc_jlDataEmprestimoDescricao.anchor = GridBagConstraints.WEST;
		gbc_jlDataEmprestimoDescricao.gridx = 2;
		gbc_jlDataEmprestimoDescricao.gridy = 4;
		jpDados.add(jlDataEmprestimoDescricao, gbc_jlDataEmprestimoDescricao);
		
		GridBagConstraints gbc_jlDataDevolucao = new GridBagConstraints();
		gbc_jlDataDevolucao.anchor = GridBagConstraints.EAST;
		gbc_jlDataDevolucao.insets = new Insets(0, 0, 5, 5);
		gbc_jlDataDevolucao.gridx = 1;
		gbc_jlDataDevolucao.gridy = 5;
		jlDataDevolucao.setFont(new Font("Tahoma", Font.BOLD, 11));
		jpDados.add(jlDataDevolucao, gbc_jlDataDevolucao);
		
		GridBagConstraints gbc_jlDataDevolucaoDescricao = new GridBagConstraints();
		gbc_jlDataDevolucaoDescricao.insets = new Insets(0, 0, 5, 0);
		gbc_jlDataDevolucaoDescricao.anchor = GridBagConstraints.WEST;
		gbc_jlDataDevolucaoDescricao.gridx = 2;
		gbc_jlDataDevolucaoDescricao.gridy = 5;
		jpDados.add(jlDataDevolucaoDescricao, gbc_jlDataDevolucaoDescricao);
		
		GridBagConstraints gbc_jlCliente = new GridBagConstraints();
		gbc_jlCliente.anchor = GridBagConstraints.EAST;
		gbc_jlCliente.insets = new Insets(0, 0, 5, 5);
		gbc_jlCliente.gridx = 1;
		gbc_jlCliente.gridy = 6;
		jlCliente.setFont(new Font("Tahoma", Font.BOLD, 11));
		jpDados.add(jlCliente, gbc_jlCliente);
		
		GridBagConstraints gbc_JlClienteDescricao = new GridBagConstraints();
		gbc_JlClienteDescricao.anchor = GridBagConstraints.WEST;
		gbc_JlClienteDescricao.insets = new Insets(0, 0, 5, 0);
		gbc_JlClienteDescricao.gridx = 2;
		gbc_JlClienteDescricao.gridy = 6;
		jpDados.add(JlClienteDescricao, gbc_JlClienteDescricao);
		
		GridBagConstraints gbc_jlCPF = new GridBagConstraints();
		gbc_jlCPF.anchor = GridBagConstraints.NORTHEAST;
		gbc_jlCPF.insets = new Insets(0, 0, 0, 5);
		gbc_jlCPF.gridx = 1;
		gbc_jlCPF.gridy = 7;
		jlCPF.setFont(new Font("Tahoma", Font.BOLD, 11));
		jpDados.add(jlCPF, gbc_jlCPF);
		
		GridBagConstraints gbc_jlCPFDescricao = new GridBagConstraints();
		gbc_jlCPFDescricao.anchor = GridBagConstraints.NORTHWEST;
		gbc_jlCPFDescricao.gridx = 2;
		gbc_jlCPFDescricao.gridy = 7;
		jpDados.add(jlCPFDescricao, gbc_jlCPFDescricao);
		JPanel buttonPane = new JPanel();
		buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
		getContentPane().add(buttonPane, BorderLayout.SOUTH);
		jbLimpar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				controller.btnLimparEventHandler();
			}
		});
		
		buttonPane.add(jbLimpar);
		jbFechar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				controller.btnFecharEventHandler();
			}
		});
		buttonPane.add(jbFechar);
		jbConfirmar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				controller.btnConfirmarEventHandler();
			}
		});
		buttonPane.add(jbConfirmar);
		
		Dimension tamanhoBotoes = new Dimension(115,25);
		jbLimpar.setPreferredSize(tamanhoBotoes);
		jbFechar.setPreferredSize(tamanhoBotoes);
		jbConfirmar.setPreferredSize(tamanhoBotoes);
		
		jbLimpar.setIcon(ImageLoader.createImageIcon("btnLimpar115x25.png"));
		jbFechar.setIcon(ImageLoader.createImageIcon("btnFechar115x25.png"));
		jbConfirmar.setIcon(ImageLoader.createImageIcon("btnConfirmar115x25.png"));
		
		controller = new DevolucaoExemplarController(this);
		
		habilitarBtnConfirmar(false);
		limparFormulario();
	}
	
	public void limparFormulario(){
		JtCodExemplar.setText("");
		jlTituloDescricao.setText("");
		jlISBNDescricao.setText("");
		jlPrevDevolucaoDescricao.setText("");
		jlDataEmprestimoDescricao.setText("");
		jlDataDevolucaoDescricao.setText("");
		JlClienteDescricao.setText("");
		jlCPFDescricao.setText("");				
	}
	
	public void exibirDadosEmprestimo(){
		if (emprestimoSelecionado != null){
			Exemplar exemplar = emprestimoSelecionado.getExemplar();
			Livro livro = exemplar.getLivro();
			Pessoa cliente = emprestimoSelecionado.getPessoa();
			
			jlTituloDescricao.setText(livro.getTitulo());
			jlISBNDescricao.setText(livro.getIsbn13());
			
			jlPrevDevolucaoDescricao.setText(dateToStr(emprestimoSelecionado.getDtPrevistaDevolucao()));
			jlDataEmprestimoDescricao.setText(dateToStr(emprestimoSelecionado.getDhEmprestimo()));
			jlDataDevolucaoDescricao.setText(dateToStr(emprestimoSelecionado.getDhDevolucao()));
			
			JlClienteDescricao.setText(cliente.getNome());
			jlCPFDescricao.setText(cliente.getCPF().getNumero());
		}		
	}

	public void setEmpresimoSelecionado(Emprestimo empresimoSelecionado) {
		this.emprestimoSelecionado = empresimoSelecionado;
	}

	public Emprestimo getEmpresimoSelecionado() {
		return emprestimoSelecionado;
	}
	
	private String dateToStr(Date data){
		SimpleDateFormat sdf = new SimpleDateFormat ("dd/MM/yyyy");  
        return sdf.format(data);
	}
	
	public String getCodExemplarInformado(){
		return JtCodExemplar.getText();
	}
	
	public void requestFocusjtCodExemplar(){
		JtCodExemplar.requestFocus();
	}
	
	public void habilitarBtnConfirmar(boolean habilitar){
		jbConfirmar.setEnabled(habilitar);
	}

}
